package org.example.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 *@ClassName: SendMessageController
 *@Author: CJ
 *@Date: 2021-9-23 17:19
 */
@Controller
public class SendMessageController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * DirectExchange模式接口
     *
     * @Author: CJ
     * @Date: 2021-9-24 20:22
     * @return: java.lang.String
     */
    @GetMapping("/sendDirectMessage")
    @ResponseBody
    public String sendDirectMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        /*将消息携带绑定键值DirectRouting 发送到交换机directExchange上*/
        rabbitTemplate.convertAndSend("directExchange", "DirectRouting", map);
        return "消息已发送至rabbitmq server";
    }

    /**
     * TopicExchange模式接口
     *
     * @Author: CJ
     * @Date: 2021-9-24 20:23
     * @return: java.lang.String
     */
    @GetMapping("/sendTopicMessage1")
    @ResponseBody
    public String sendTopicMessage1() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: M A N ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> manMap = new HashMap<>();
        manMap.put("messageId", messageId);
        manMap.put("messageData", messageData);
        manMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend("topicExchange", "topic.man", manMap);
        return "消息已发送至rabbitmq server";
    }

    /**
     * TopicExchange模式接口
     *
     * @Author: CJ
     * @Date: 2021-9-24 20:24
     * @return: java.lang.String
     */
    @GetMapping("/sendTopicMessage2")
    @ResponseBody
    public String sendTopicMessage2() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: woman is all ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> womanMap = new HashMap<>();
        womanMap.put("messageId", messageId);
        womanMap.put("messageData", messageData);
        womanMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend("topicExchange", "topic.woman", womanMap);
        return "消息已发送至rabbitmq server";
    }

    /**
     * FanoutExchange模式接口
     *
     * @Author: CJ
     * @Date: 2021-9-24 20:24
     * @return: java.lang.String
     */
    @GetMapping("/sendFanoutMessage")
    @ResponseBody
    public String sendFanoutMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: testFanoutMessage";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("fanoutExchange", null, map);
        return "消息已发送至rabbitmq server";
    }

    /**
     * 消息发送确认接口
     *
     * @Author: CJ
     * @Date: 2021-9-24 20:25
     * @return: java.lang.String
     */
    @GetMapping("/TestMessageAck")
    @ResponseBody
    public String TestMessageAck() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: non-existent-exchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("non-existent-exchange", "TestDirectRouting", map);
        return "消息已发送至rabbitmq server";
    }

    /**
     * 消息发送确认接口
     *
     * @Author: CJ
     * @Date: 2021-9-24 20:26
     * @return: java.lang.String
     */
    @GetMapping("/TestMessageAck2")
    @ResponseBody
    public String TestMessageAck2() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: lonelyDirectExchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("lonelyDirectExchange", "TestDirectRouting", map);
        return "消息已发送至rabbitmq server";
    }

    /**
     * 死信队列接口1
     *
     * @Author: CJ
     * @Date: 2021-9-25 13:20
     * @return: java.lang.String
     */
    @GetMapping("/sendDeadLetterMessage1")
    @ResponseBody
    public String sendDeadLetterMessage1() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: dead.letter.business.exchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("dead.letter.business.exchange", "dead.letter.business.queuea", map);
        return "消息已发送至rabbitmq server";
    }
}
